package com.yoomiito.app.ui.my.pick;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.jakewharton.rxbinding3.view.RxView;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.PickGoodsAdapter;
import com.yoomiito.app.address.AddressListActivity;
import com.yoomiito.app.address.EditAddressActivity;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.interf.BaseOrderGoodsView;
import com.yoomiito.app.model.bean.AddressInfo;
import com.yoomiito.app.model.bean.AppInterfaceInfo;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.model.bean.GoodsList;
import com.yoomiito.app.model.my.MyRemainMoneyInfo;
import com.yoomiito.app.ui.anyview.order.BaoPinPinOrderGoodsView;
import com.yoomiito.app.ui.anyview.order.GiftOrderGoodsView;
import com.yoomiito.app.ui.anyview.order.ShowAddressView;
import com.yoomiito.app.ui.anyview.order.StarStoreGoodsOrderView;
import com.yoomiito.app.ui.anyview.order.YouPinOrderGoodsView;
import com.yoomiito.app.ui.my.psw.SetPayPswActivity;
import com.yoomiito.app.ui.order.UseCouponDialog;
import com.yoomiito.app.ui.order.sureorder.BuyResultActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.Const;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.DialogManager;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.TouchUtils;
import com.yoomiito.app.utils.Type;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.utils.encrypt.AESUtils;
import com.yoomiito.app.utils.inputfilter.TextInputFilter;
import com.yoomiito.app.widget.PasswordView_1;
import com.yoomiito.app.widget.TipDialog;
import com.yoomiito.app.widget.dialog.NewVersionDialog;
import com.yoomiito.app.widget.dialog.PayDialog;
import com.yoomiito.app.widget.dialog.PickDialog;
import com.yoomiito.app.widget.dialog.PswInputDialog;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.LoadingType;
import com.yoomiito.app.widget.state.StatusManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * 确认订单
 */
public class PickGoodsActivity extends BaseActivity<PickGoodsPresenter> {


    @BindView(R.id.address_container)
    FrameLayout addressParentView;
    @BindView(R.id.rv_goods)
    RecyclerView recyclerView;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;


    @BindView(R.id.act_buy_goods_notes)
    EditText notesTv;

    private PickDialog dialog;

    private AddressInfo mAddressInfo;
    private ShowAddressView mShowAddressView;
    private PickGoodsAdapter mAdapter;


    @Override
    public View getLayoutView() {
        mStatusManager = new StatusManager.Builder(context)
                .addType(new ContentType(R.layout.act_pick_goods))
                .addType(new LoadingType())
                .addType(new EmptyType().setEmptyContent("无提货商品"))
                .build();
        return mStatusManager.view();
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public PickGoodsPresenter newP() {
        return new PickGoodsPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PickGoodsAdapter();
        recyclerView.setAdapter(mAdapter);
        getP().getAddress();
        getP().confirmOrder();
    }


    @OnClick({R.id.act_buy_goods_back, R.id.btn_pick})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.act_buy_goods_back:
                finish();
                break;
            case R.id.btn_pick:
                List<GoodsList> lists = new ArrayList<>();
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (mAdapter.getData().get(i).setNum > 0) {
                        GoodsList goodsDetail = new GoodsList();
                        goodsDetail.num = mAdapter.getData().get(i).setNum;
                        goodsDetail.productName = mAdapter.getData().get(i).getTitle();
                        goodsDetail.unit = mAdapter.getData().get(i).unit;
                        goodsDetail.productId = mAdapter.getData().get(i).id;
                        lists.add(goodsDetail);
                    }
                }
                if (lists.size() == 0) {
                    YMToast.showText("请确认你要提货的商品");
                    return;
                }
                if (dialog == null) {
                    dialog = new PickDialog(this)
                            .setOnClickListener(isSure -> {
                                if(isSure){
                                    String note = notesTv.getText().toString().trim();
                                    getP().createOrder(new Gson().toJson(lists), mAddressInfo.getUserName(), mAddressInfo.getProvinceName(),
                                            mAddressInfo.getCityName(), mAddressInfo.getCountyName(), mAddressInfo.getMobile(), mAddressInfo.getAddress(),
                                            note);
                                }else {
                                    AddressListActivity.Companion.startForResult(this, mAddressInfo, Const.RequestCode.AddressCode);
                                }
                            });
                }
                dialog.setAddress(mAddressInfo,lists,notesTv.getText().toString());

                dialog.show();

                break;

        }
    }


    public void showAddressView(AddressInfo addressInfo) {
        dismissLoading();
        mShowAddressView = new ShowAddressView(this);
        addressParentView.addView(mShowAddressView);
        mAddressInfo = addressInfo;
        if (mAddressInfo != null && mAddressInfo.getId() != null) {
            mShowAddressView.setAddressInfo(mAddressInfo);
            mShowAddressView.showAddress();
        }
    }

    public void setData(List<GoodsDetail> data) {
        if (data.size() == 0) {
            llEmpty.setVisibility(View.VISIBLE);
        } else {
            llEmpty.setVisibility(View.GONE);
            mAdapter.setNewData(data);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Const.RequestCode.AddressCode == requestCode && data != null) {
            mAddressInfo = (AddressInfo) data.getSerializableExtra(Constant.DATA);
            L.i("地址：" + mAddressInfo.toString());
            mShowAddressView.setAddressInfo(mAddressInfo);
            mShowAddressView.showAddress();
//            getP().getAddress();
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
